package com.heyun.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heyun.bean.HeyunSalesDeptAnal;
import com.heyun.bean.Message;
import com.heyun.bean.PieData;
import com.heyun.bean.SeriesPie;
import com.heyun.service.impl.SalesAnalServiceImpl;
import com.mysql.jdbc.PreparedStatement.ParseInfo;

@Controller
@RequestMapping("/SalesAnal")
public class SalesAnalController {
	@Autowired
	private SalesAnalServiceImpl sasi;
	private PieData pieChartData;

	@RequestMapping("/getSalesAnal")
	@ResponseBody
	public Map<String, Object> getSalesAnal(int currentPage) {
		System.out.println("===========月销售金额展示控制层===========");
		Map<String, Object> map = sasi.getSalesAnal(currentPage);
		return map;
	}

	@RequestMapping("/getSalesDeptAnal")
	@ResponseBody
	public Map<String, Object> getSalesDeptAnal(int salesMonthAnalId,
			int currentPage) {
		System.out.println("===========部门销售金额展示控制层===========");
		System.out.println("月销售id是==============" + salesMonthAnalId);
		Map<String, Object> map = sasi.getSalesDeptAnal(salesMonthAnalId,
				currentPage);
		return map;
	}

	/*-----------------------------------------------得到饼状图的数据对象----------------------------------------------*/
	@RequestMapping("/getPieData")
	@ResponseBody
	public PieData getPieData(int salesMonthAnalId) {
		List<HeyunSalesDeptAnal> hsdalist = sasi.getSalesDeptAnal(salesMonthAnalId);
		List<SeriesPie> serieslist=new ArrayList<SeriesPie>();
		
		for (HeyunSalesDeptAnal hsda : hsdalist) {
			SeriesPie e=new SeriesPie();
			e.setName(hsda.getDeptName());
			e.setValue(hsda.getSalesMoney());
			serieslist.add(e);
		}
		for(SeriesPie e:serieslist){
			System.out.println(">>>>>>>>>>>>>>"+e.getName());
			System.out.println(">>>>>>>>>>>>>>"+e.getValue());
		}
		// 封装图例
		List<String> legend = new ArrayList<String>();
		for (SeriesPie series : serieslist) {
			legend.add(series.getName());
			
		}
		pieChartData = new PieData(legend, serieslist);
		return pieChartData;
	};

	/*-----------------------------------------------导出文件----------------------------------------------*/
	@RequestMapping("/createExcel")
	@ResponseBody
	public void createExcel(int salesMonthAnalId,HttpServletResponse response) {
		try {
			System.out.println("============准备生成 Excel报表===============");
			// 获取响应的 输出流
			OutputStream out = response.getOutputStream();
			// 设置下载的文件类型
			response.setContentType("application/vnd.ms-excel");
			// 设置Header,可以设置下载附件的文件名
			response.setHeader("Content-Disposition", "attachment; filename="
					+ new String(("部门销售金额列表.xls").getBytes(), "ISO-8859-1"));
			// 创建一个工作簿对象
			WritableWorkbook book = Workbook.createWorkbook(out);
			// 创建 一个 或多个 工作表,指定工作簿选项卡的名称，以及 工作表的 索引位置
			WritableSheet sheet1 = book.createSheet("a", 0);

			// 创建一个字体对象，封装指定的字体
			WritableFont font1 = new WritableFont(
					WritableFont.createFont("隶书"), 15);
			font1.setColour(Colour.RED);
			// 创建一个 单元格 格式对象，封装指定格式
			WritableCellFormat wcTitle = new WritableCellFormat();
			wcTitle.setFont(font1);
			wcTitle.setAlignment(Alignment.CENTRE);
			// 向工作表中 添加单元格，单元格 内容 需要借助于 Label对象 或 Number对象进行封装
			sheet1.addCell(new Label(0, 0, "部门销售金额列表", wcTitle));
			sheet1.mergeCells(0, 0, 2, 0);
			sheet1.addCell(new Label(0, 1, "部门"));
			sheet1.addCell(new Label(1, 1, "销售金额(单位/万元)"));
			sheet1.addCell(new Label(2, 1, "同比"));
			sheet1.addCell(new Label(3, 1, "环比"));
			System.out.println("++++++++++++++++++++++"+salesMonthAnalId);
			List<HeyunSalesDeptAnal> hsdalist = sasi.getSalesDeptAnal(salesMonthAnalId);
			System.out.println("---------------------");
			for (int i = 0; i < hsdalist.size(); i++) {
				HeyunSalesDeptAnal hsda = hsdalist.get(i);
				sheet1.addCell(new Label(0, (i + 2), hsda.getDeptName()));
				sheet1.addCell(new Label(1, (i + 2), String.valueOf(hsda.getSalesMoney())));
				sheet1.addCell(new Label(2, (i + 2), hsda.getAn()));
				sheet1.addCell(new Label(3, (i + 2), hsda.getMom()));
			}
			// 将工作簿对象 输出到磁盘
			book.write();
			book.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/findSalesAnalByMonth")
	@ResponseBody
	public Message findSalesAnalByMonth(int currentPage,String month){
        System.out.println("当前页是====="+currentPage+"要查询的月份是====="+month);
		Map<String, Object> map = sasi.findSalesAnalByMonth(currentPage, month);
		return Message.success().add("map", map);
		
	}
}
